package cn.zzp.user.service.impl;

import cn.zzp.basic.constant.VerifyCodeConstants;
import cn.zzp.basic.exception.BusinessException;
import cn.zzp.basic.service.impl.BaseServiceImpl;
import cn.zzp.basic.util.Md5Utils;
import cn.zzp.basic.util.StrUtils;
import cn.zzp.user.domain.LoginInfo;
import cn.zzp.user.domain.User;
import cn.zzp.user.dto.UserDto;
import cn.zzp.user.mapper.LoginInfoMapper;
import cn.zzp.user.mapper.UserMapper;
import cn.zzp.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @ClassName EmployeeServiceImpl
 * @Description
 * @Author 祝志鹏
 * @Date 2022/7/14 14:16
 */
@Service
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    /**
     * 手机注册
     * @param userDto
     */
    @Transactional
    @Override
    public void phoneRegister(UserDto userDto) {
        // 1. 判断参数是否有效
        String phone = userDto.getPhone();
        String code = userDto.getCode();
        String password = userDto.getPassword();
        String passwordRepeat = userDto.getPasswordRepeat();

        if (
                StringUtils.isEmpty(phone)
                || StringUtils.isEmpty(code)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(passwordRepeat)
        ){
            throw new BusinessException("参数异常，请确认填写无误！");
        }

        // 2. 判断密码和确认密码是否一致
        if (!password.equals(passwordRepeat)){
            throw new BusinessException("两次输入密码不同，请重新确认输入");
        }

        // 3. 判断验证码是否过期
        Object codeValues = redisTemplate.opsForValue().get(phone + VerifyCodeConstants.PHONE_REGISTER_SUFFIX);
        if (codeValues == null){
            throw new BusinessException("手机验证码已经过期，请重新获取");
        }
        // 4. 判断验证码是否正确
        String redisCode = codeValues.toString().split(":")[0];
        if (!redisCode.equalsIgnoreCase(code)){
            throw new BusinessException("手机验证码错误，请重新输入");
        }

        // 5. 保存数据
        LoginInfo loginInfo = dto2LoginInfo(userDto);
        User user = loginInfo2User(loginInfo);

        // 先添加loginInfo获取自增id，添加进入user中
        loginInfoMapper.add(loginInfo);

        user.setLogininfo_id(loginInfo.getId());
        userMapper.add(user);

    }

    /**
     * 通过UserDto给LoginInfo赋值
     * @param dto
     * @return
     */
    private LoginInfo dto2LoginInfo (UserDto dto) {
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUsername(dto.getPhone());
        loginInfo.setPhone(dto.getPhone());
        loginInfo.setType(1);

        // 密码单独处理，需要设置加密
        // 生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 密码加密 加盐加密
        String pwd = Md5Utils.encrypByMd5(salt + dto.getPassword());

        loginInfo.setSalt(salt);
        loginInfo.setPassword(pwd);

        return loginInfo;

    }

    /**
     * 通过LoginInfo给User赋值
     * @param loginInfo
     * @return
     */
    private User loginInfo2User(LoginInfo loginInfo){

        // 同属性字段可以直接调用BeanUtils工具类进行赋值
        User user = new User();
        BeanUtils.copyProperties(loginInfo,user);

        return user;
    }

    /**
     * 添加操作
     * @param user
     */
    @Transactional
    @Override
    public void add(User user){
        LoginInfo loginInfo = getLoginInfo(user);

        loginInfoMapper.add(loginInfo);

        user.setLogininfo_id(loginInfo.getId());
        userMapper.add(user);

    }

    /**
     * 更新
     * @param user
     */
    @Transactional
    @Override
    public void update(User user){
        LoginInfo loginInfo = getLoginInfo(user);

        Long logininfo_id = user.getLogininfo_id();
        loginInfo.setId(logininfo_id);

        loginInfoMapper.update(loginInfo);
        userMapper.update(user);
    }

    /**
     * 删除
     * @param id
     */
    @Transactional
    @Override
    public void delete(Long id){
        Long logininfo_id = userMapper.queryById(id).getLogininfo_id();

        loginInfoMapper.delete(logininfo_id);
        userMapper.delete(id);
    }


    /**
     * 设置盐值密码和同步loginInfo
     * @param user
     * @return
     */
    private LoginInfo getLoginInfo(User user){

        // 获取盐值
        String salt = StrUtils.getComplexRandomString(32);

        // 获取加密密码
        String password = Md5Utils.encrypByMd5(salt + user.getPassword());

        user.setSalt(salt);
        user.setPassword(password);
        // 同步LoginInfo
        return user2LoginInfo(user);


    }

    /**
     * 通过User对象给LoginInfo赋值
     * @param user
     * @return
     */
    private LoginInfo user2LoginInfo(User user) {

        LoginInfo loginInfo = new LoginInfo();

        BeanUtils.copyProperties(user, loginInfo);
        // 设置普通用户权限
        loginInfo.setType(1);

        return loginInfo;
    }
}
